package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.AfterSaleService;
import com.neusoft.ecommerce.entity.Goods;
import com.neusoft.ecommerce.entity.GoodsAfterSale;
import com.neusoft.ecommerce.service.GoodsAfterSaleService;
import com.neusoft.ecommerce.service.GoodsService;

/**
 * 
 * @author YinKuenTam
 *
 */
@RestController
public class GoodsAfterSaleController {
	private final GoodsAfterSaleService goodsAfterSaleService;
	private final GoodsService goodsService;
	
	public GoodsAfterSaleController(GoodsAfterSaleService goodsAfterSaleService,GoodsService goodsService) {
		this.goodsAfterSaleService = goodsAfterSaleService;
		this.goodsService = goodsService;
			
	}
	@GetMapping("/goodsAfterSale_page")
    public ModelAndView afterSaleServicePage() {
        System.out.println("-----sss______");
         return new ModelAndView("goodsAfterSale/goodsAfterSale-list");
    }
	
    @RequestMapping("/goodsAfterSale/list")
    @ResponseBody
    public LayuiPager<GoodsAfterSale> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<GoodsAfterSale> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = goodsAfterSaleService.findTotal(pager);
        List<GoodsAfterSale> list = goodsAfterSaleService.findList(pager);
        for (GoodsAfterSale goodsAfterSale : list) {
            System.out.println(goodsAfterSale);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }
	
	@GetMapping("/addGoodsAfterSale")
    public ModelAndView addAfterSaleService() {
        return new ModelAndView("goodsAfterSale/goodsAfterSale-add");
    }
	
	@RequestMapping("/goodsAfterSale/goodsList")
    public List<Goods> findGoodsList() {
	    return goodsService.findAll();
    }
	
	@PostMapping("/goodsAfterSale/add")
    public Map<String, Object> add(GoodsAfterSale goodsAfterSale) {
        System.out.println(goodsAfterSale);
        Map<String, Object> map = new HashMap<>(16);
        if (goodsAfterSaleService.add(goodsAfterSale)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
	@GetMapping("/goodsAfterSale/edit_page")
    public ModelAndView editPage(GoodsAfterSale goodsAfterSale, Model model) {
        System.out.println(goodsAfterSale);
        LayuiPager<GoodsAfterSale> pager = new LayuiPager<>(1,1);
        pager.setCondition(goodsAfterSale);
        GoodsAfterSale message = goodsAfterSaleService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("goodsAfterSale/goodsAfterSale-edit");
        model.addAttribute("goodsAfterSale", message);
        return view;
    }


	@PostMapping("/goodsAfterSale/edit")
    public Map<String, Object> edit(GoodsAfterSale goodsAfterSale) {
        System.out.println(goodsAfterSale);
        Map<String, Object> map = new HashMap<>(16);
        if (goodsAfterSaleService.edit(goodsAfterSale)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/goodsAfterSale/delete")
    public Map<String, Object> delete(GoodsAfterSale goodsAfterSale) {
        System.out.println(goodsAfterSale);
        Map<String, Object> map = new HashMap<>(16);
        if (goodsAfterSaleService.delete(goodsAfterSale)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

}
